﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class player : MonoBehaviour {      //player是子类 继承Monobehaviour父类的变量 然后再加新的东西
    public Rigidbody rb;         //刚体实例化一个变量
    public float speed;
    public GameObject pickup;
    int score;
    public Text scoreText;
	// Use this for initialization
	void Start () {
        rb = GetComponent<Rigidbody>();
        CreatePickUp();
        
        scoreText.text = "";
        score = 0;
        SetText();
	}
    private void OnTriggerEnter( Collider other)  //C++中 什么也不加默认private 碰到有触发器的物体onTriggerEnter other 为那个物体的碰撞器 
    {
        if (other.tag == "pick_up")
        {
            Destroy(other.gameObject); //gameObject在之前已经被实例化了 其实也是GameObject实例化的内容 gameObject直接就是一个Collider的一个标签类，直接可以作为一个变量用
            CreatePickUp();
            score++;
            SetText();
        }
    }
	void FixedUpdate(){

    }
	// Update is called once per frame
	void Update () {
        float h, v;
        h = Input.GetAxis("Horizontal");
        v = Input.GetAxis("Vertical");
        rb.AddForce(new Vector3(h*speed, 0*speed, v*speed));
	}
    void CreatePickUp()
    {
        GameObject obj = (GameObject)Instantiate(pickup);
        obj.transform.position = new Vector3(Random.Range(-12,12),2.81f,Random.Range(-12,12));    //范围
    }
    void SetText()
    {
        scoreText.text = "Score:" + score.ToString();//字符串可以相加
    }
}
